package rhinestone.TUI;

import java.util.Iterator;

import rhinestone.Control.SaleController;
import rhinestone.Model.Item;
import rhinestone.Model.Sale;
import rhinestone.Model.Product;
import rhinestone.Model.Pack;

/**
 * SaleUI is responsible for sale user interface.
 * It prints the menu and prompts user inputs whenever needed.
 *
 * Whenever the user gets here, the new empty sale is created
 * and with the options provided in the class the users fills it up.
 * In the sale is checked out and put in a sale container.
 *
 * @author Group 2
 * @version 2010.12.15
 */
public class SaleUI extends UI {
	private SaleController controller;
	private String receipt;

	/**
	 * Constructor which takes the controller as a parameter.
	 * @param controller The controller the UI is sending the users input to.
	 */
	public SaleUI(SaleController controller) {
		this.controller = controller;
	}

	/**
	 * This method holds the logic of the UI.
	 * Depending on the users input, it calls particular method of controller.
	 */
	public void startMenu() {
		controller.initializeSale();
		boolean finished = false;
		while(!finished) {
			printMenu();
			int choice = promptChoiceInput();
			switch(choice) {
				case 1:
					scanItemChoice();
					break;
				case 2:
					checkoutSaleChoice();
					finished = true;
					break;
				case 3:
					removeItemChoice();
					break;
				case 4:
					cancelSaleChoice();
					finished = true;
					break;
				default:
					break;
			}
		}
	}

	/**
	 * The menu the UI prints to the user.
	 */
	protected void printMenu() {
		System.out.println(" ----- CHECKOUT MENU ----- ");
		System.out.println("\t(-1-) Scan item");
		System.out.println("\t(-2-) Checkout items");
		System.out.println("\t(-3-) Remove item");
		System.out.println("\t(-4-) Cancel checkout");
	}

	/**
	 * Down there are a methods which are called by startMenu method
	 * depending on the users choice.
	 */
	private void scanItemChoice() {
		String barcode = promptStringInput(BARCODE_MESSAGE);
		int amount = promptIntInput(AMOUNT_MESSAGE);
		try {
			Item item = controller.addItem(barcode, amount);
			if(item != null)
				printItem(item, amount);
		} catch(Exception e) {}
	}

	private void checkoutSaleChoice() {
		String cpr = promptStringInput(CPR_MESSAGE);
		Sale sale = controller.checkoutItems(cpr);
		if(sale != null)
			printSaleReceipt(sale);
		else
			System.err.println("The purchase is empty.\nExiting to main menu.");
	}

	private void removeItemChoice() {
		String barcode = promptStringInput(BARCODE_MESSAGE);
		int amount = promptIntInput(AMOUNT_MESSAGE);
		controller.removeItem(barcode, amount);
	}

	private void cancelSaleChoice() {
		controller.cancelSale();
		receipt = "";
	}

	public void printItem(Item item, int amount) {
		String output = amount + "x " + item.getName() + " -- " + (amount * item.getPrice());
		System.out.println(output);
	}

	public void printSaleReceipt(Sale sale) {
		receipt = "";
		receipt += "\n\t-------------------------------------";
		receipt += "\n\t------------- RECEIPT ---------------";
		receipt += "\n\t-------------------------------------";
		Iterator<Product> products = sale.getProducts();
		while(products.hasNext()) {
			Product product = products.next();
			receipt += "\n\t" + sale.getAmount(product) + "x " + product.getName() + " -- " + (sale.getAmount(product) * product.getPrice());
		}
		Iterator<Pack> packs = sale.getPacks();
		while(packs.hasNext()) {
			Pack pack = packs.next();
			receipt += "\n\t" + sale.getAmount(pack) + "x " + pack.getName() + " -- " + (sale.getAmount(pack) * pack.getPrice());
		}
		receipt += "\n\t-------------------------------------";
		receipt += "\n\tTotal price: $" + sale.getTotalPrice();
		if(sale.hasCustomer()) {
			receipt += "\n\tApplying customers discount...";
			receipt += "\n\tTotal price with discount: $" + sale.applyCustomerDiscount(sale.getTotalPrice());
		}
		System.out.println(receipt);
	}
}
